package com.config;

import com.framework.MessageConverter.UserMessageConverter;
import com.service.ISayHello;
import com.serviceImpl.SayHelloImpl;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.jms.remoting.JmsInvokerServiceExporter;
import org.springframework.jms.support.converter.MessageConverter;

@Configuration
@PropertySource({"classpath:setting.properties"})
@ComponentScan(basePackages = {"com.serviceImpl"})
public class JmsInvokerServiceExporterConfig {

    @Autowired
    private Environment environment;

    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL( environment.getProperty( "activeMQ.brokerUrl" ) );
        activeMQConnectionFactory.setTrustAllPackages( true ); //activeMQ针对对象的传输有白名单限制（即使你的类实现了序列化借口也会报错），需要将你类所在的包加入白名单。在接受的时候将activeMQConnection.setTrustAllPackages(true);设置为true，（默认的是fales）。
        return activeMQConnectionFactory;
    }


    //=====================message converter===========
    @Bean
    public MessageConverter mappingJacksonMessageConverter() {
        return new UserMessageConverter();
    }


    //==================== 服务导出==============
    @Bean
    public JmsInvokerServiceExporter sayHelloServiceExporter(SayHelloImpl sayHelloImpl, MessageConverter messageConverter) {
        JmsInvokerServiceExporter jmsInvokerServiceExporter = new JmsInvokerServiceExporter();
        jmsInvokerServiceExporter.setIgnoreInvalidRequests( true );
        jmsInvokerServiceExporter.setMessageConverter( messageConverter );
        jmsInvokerServiceExporter.setService( sayHelloImpl );
        jmsInvokerServiceExporter.setServiceInterface( ISayHello.class );
        return jmsInvokerServiceExporter;
    }

    @Bean
    public MessageListenerAdapter messageListenerAdapter(JmsInvokerServiceExporter jmsInvokerServiceExporter) {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter( jmsInvokerServiceExporter );
        return messageListenerAdapter;
    }


    @Bean
    public DefaultMessageListenerContainer messageListenerContainer(ActiveMQConnectionFactory activeMQConnectionFactory, MessageConverter messageConverter, MessageListenerAdapter messageListenerAdapter) {
        DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
        messageListenerContainer.setConnectionFactory( activeMQConnectionFactory );
        messageListenerContainer.setMessageConverter( messageConverter );
        messageListenerContainer.setDestinationName( environment.getProperty( "activeMQ.service.queue.name" ) );
        messageListenerContainer.setMessageListener( messageListenerAdapter );
        return messageListenerContainer;
    }


}
